<?php
class ModelCatalogProduct extends Model{
    
    public function getNewProductListReceive($type, $start = 0, $limit = 16){
        
        if($type == 'newReceived'){
            $query = $this->db->query("
            SELECT p.product_id, p.image, p.price, p.date_added, pdes.name, pdes.duration, pdis.price AS disprice, pdis.date_start, pdis.date_end 
            FROM product p LEFT JOIN product_description pdes ON p.product_id = pdes.product_id LEFT JOIN product_discount pdis ON p.product_id = pdis.product_id 
            ORDER BY p.product_id DESC LIMIT " . (int)$start . "," . (int)$limit);
        }else if($type == 'special'){
            //TODO : rewrite SQL HERE FOR SPECIAL
            
            $query = $this->db->query("
            SELECT p.product_id, p.viewed, p.image, p.price, p.date_added, pdes.name, pdes.duration, pdis.price AS disprice, pdis.date_start, pdis.date_end 
            FROM product p LEFT JOIN product_description pdes ON p.product_id = pdes.product_id LEFT JOIN product_discount pdis ON p.product_id = pdis.product_id 
            ORDER BY p.viewed, p.product_id DESC LIMIT " . (int)$start . "," . (int)$limit);
        }else if($type == 'full'){
            $query = $this->db->query("SELECT * FROM" . DB_PREFIX . "product DESC LIMIT " . (int)$start . "," . (int)$limit);
        }
        return $query->rows;
    }
    //get product by category ID
    public function get_product_by_categroryID($category_id, $start = 0, $limit = 17)
    {
        $query = $this->db->query("
            SELECT p.product_id, p.viewed, p.image, p.price, p.date_added, pdes.name, pdes.duration, pdis.price AS disprice, pdis.date_start, pdis.date_end 
            FROM product p LEFT JOIN product_description pdes ON p.product_id = pdes.product_id LEFT JOIN product_discount pdis ON p.product_id = pdis.product_id 
            WHERE p.category_id = '$category_id'
            GROUP BY p.product_id
            ORDER BY p.viewed, p.product_id DESC LIMIT " . (int)$start . "," . (int)$limit);
        
        return $query->rows;
    }
    //get product by category ID
    public function get_total_product_by_categroryID($category_id)
    {
        $query = $this->db->query("SELECT product_id FROM product WHERE category_id = '$category_id'");
        $total = count($query->rows);
        return $total;
    }
    //get all product for counting
    public function get_total_all_product()
    {
        $query = $this->db->query("SELECT product_id FROM product");
        $total = count($query->rows);
        return $total;
    }
}
?>